//
//  SISSTakeLookDetailViewController.m
//  SISSAPP
//
//  Created by 优房网 on 2017/7/13.
//  Copyright © 2017年 优房网. All rights reserved.
//

#import "SISSTakeLookDetailViewController.h"
#import "SISSMorePopupView.h"
#import "SISSTitleView.h"
#import "SISSShowView.h"
#import "SISSPhotoBrowser.h"
#import "SISSNavigationController.h"
#import "SISSComplaintViewController.h"
#import "SISSCommentView.h"

#import "SISSAddCoopCommentViewController.h"

@interface SISSTakeLookDetailViewController ()<SISSMorePopupViewDelegate,SDCycleScrollViewDelegate,SISSCommentViewDelegate,SISSCoopCommentShowViewDelegate,MWPhotoBrowserDelegate>

/* containerView */
@property (weak, nonatomic) SISSBaseView *containerView;

@end

@implementation SISSTakeLookDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"看房详情";
    // 获取带看详情数据
    [self requestTakeLookDetailData];
    // 获取评价数据
    [self requestCommentData];
    
    [self bottomButtonAssinment];
}


- (void)setBrokerState:(NSInteger)brokerState {
    _brokerState = brokerState;
    if (brokerState == 1) {
        self.navigationItem.rightBarButtonItem = nil;
    }
}

- (void)setImagesURLStrings:(NSArray *)imagesURLStrings {
    _imagesURLStrings = imagesURLStrings;
    self.pageLabel.hidden = imagesURLStrings.count <= 0;
    [self.cycleScrollView mas_updateConstraints:^(MASConstraintMaker *make) {
        if (imagesURLStrings.count > 0) {
            make.height.mas_equalTo(160);
        } else {
            make.height.mas_equalTo(0);
        }
    }];
}

#pragma mark - 代理
#pragma mark -- SISSMorePopupViewDelegate 删除
- (void)morePopupViewSelectedWithIndex:(NSInteger)index {
    WeakSelf
    NSString *str = @"删除后不可恢复，您确定删除吗？";
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.alignment = NSTextAlignmentLeft;
    
    NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:str attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:13], NSForegroundColorAttributeName: COLOR(0x334A60), NSParagraphStyleAttributeName: paragraphStyle}];
    
    UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"删除" message:nil preferredStyle:UIAlertControllerStyleAlert];
    
    [alertC setValue:attr forKey:@"attributedMessage"];
    
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"取消");
    }];
    
    
    [alertC addAction:cancelAction];
    
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"确定");
        
        [weakSelf deleteTakeLook];
        }];
    [alertC addAction:okAction];
    
    [self presentViewController:alertC animated:YES completion:nil];
    
}


#pragma mark -- SDCycleScrollViewDelegate 点击图片看大图
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index {
    
    MWPhotoBrowser *photoBrowser = [[MWPhotoBrowser alloc] initWithDelegate:self];
    [photoBrowser setCurrentPhotoIndex:index];
//    [photoBrowser showNextPhotoAnimated:YES];
//    [photoBrowser showPreviousPhotoAnimated:YES];
    SISSNavigationController *navigationController = [[SISSNavigationController alloc] initWithRootViewController:photoBrowser];
    navigationController.modalPresentationStyle = UIModalPresentationFullScreen;
    [self presentViewController:navigationController animated:YES completion:nil];
}

- (NSUInteger)numberOfPhotosInPhotoBrowser:(MWPhotoBrowser *)photoBrowser {
    return self.imagesURLStrings.count;
}

- (id<MWPhoto>)photoBrowser:(MWPhotoBrowser *)photoBrowser photoAtIndex:(NSUInteger)index {
    
    //创建图片模型
    MWPhoto *photo = [MWPhoto photoWithURL:[NSURL URLWithString:self.imagesURLStrings[index]]];
    return photo;
}

#pragma mark -- SISSCommentViewDelegate 评价、追加
- (void)commentViewConfirmButtonClickAction {
    
    if (self.isAdditionalComment) { // 追加
        NSLog(@"%@",self.additonalTextView.text);
        
        [self addAdditionalComment];
    } else { // 评价
        NSLog(@" %zd %zd %zd \n%@",self.coopMidView.validityStar,self.coopMidView.satisfyStar,self.coopMidView.businessLevelStar,self.coopMidView.commentTextView.text);
        [self addComment];
    }
}

#pragma mark -- SISSCoopCommentShowViewDelegate 申诉
- (void)rightAppealButtonClickAction {
    
    SISSComplaintViewController *vc = [[SISSComplaintViewController alloc] init];
    vc.acrId = self.beAcrId;
    vc.commentType = 7;
    [self.navigationController pushViewController:vc animated:YES];
    NSLog(@"申诉");
}

#pragma mark - setter

#pragma mark - 点击事件
#pragma mark -- 右上角更多按钮点击事件
- (void)rightMoreItemClickAction {
    
    SISSMorePopupView *moreView = [[SISSMorePopupView alloc] initWithTitles:@[@"删除"]];
    moreView.delegate = self;
    [moreView show];
}
#pragma mark -- 底部按钮点击事件
- (void)bottomButtonClickAction {
    
    NSLog(@"点击底部按钮");
    if (self.isAdditionalComment) {
        UIView *midView = [[UIView alloc] init];
        SISSTextView *additonalTextView = [[SISSTextView alloc] init];
        additonalTextView.layer.borderWidth = 1;
        additonalTextView.layer.borderColor = [UIColor groupTableViewBackgroundColor].CGColor;
        additonalTextView.placeholder = @"请输入追加评价，不要包含特殊字符。";
        [midView addSubview:additonalTextView];
        self.additonalTextView = additonalTextView;
        [additonalTextView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.equalTo(midView).offset(16);
            make.height.mas_equalTo(80);
            make.bottom.right.equalTo(midView).offset(-16);
        }];
        SISSCommentView *view = [[SISSCommentView alloc] initWithTitle:@"评价" midView:midView];
        view.delegate = self;
    } else {
        
        if (self.isCooperationIn) {
            SISSCoopCommentMidView *coopMidView = [[SISSCoopCommentMidView alloc] init];
            self.coopMidView = coopMidView;
            SISSCommentView *view = [[SISSCommentView alloc] initWithTitle:@"评价" midView:coopMidView];
            view.delegate = self;
        } else {
            SISSCommentMidView *midView = [[SISSCommentMidView alloc] init];
            self.midView = midView;
            SISSCommentView *view = [[SISSCommentView alloc] initWithTitle:@"评价" midView:midView];
            view.delegate = self;
        }
    }
    
//    SISSAddCoopCommentViewController *vc = [[SISSAddCoopCommentViewController alloc] init];
//    vc.takeLookModel = self.takeLookModel;
//    vc.houseCoopProgressModel = self.houseCoopProgressModel;
//    [self.navigationController pushViewController:vc animated:YES];

}


#pragma mark -- 带看是否属实确认点击事件
- (void)takeLookTrueOrNotButtonClickAction:(UIButton *)button {
    
    [self takeLookTrueOrNotWithState:button.tag - 99];
}

#pragma mark - 底部Button不同状态及赋值
- (void)bottomButtonAssinment {
    
    NSString *buttonStr;
    if (self.type == 1) {
        
        switch (self.brokerState) {
            case 0: {
                self.bottomButton.hidden = YES;
            }
                break;
                
            case 1: {
                self.bottomButton.hidden = NO;
                self.trueButton.hidden = YES;
                self.untrueButton.hidden = YES;
                buttonStr = @"评价";
            }
                break;
                
            case 2: {
                self.bottomButton.hidden = NO;
                self.trueButton.hidden = YES;
                self.untrueButton.hidden = YES;
                buttonStr = @"已否认";
                self.bottomButton.userInteractionEnabled = NO;
            }
                break;
                
            default:
                break;
        }
        
        [self.bottomButton setTitle:buttonStr forState:UIControlStateNormal];
        
    } else {
        switch (self.brokerState) {
            case 0: {
                 buttonStr = @"等待对方确认";
                self.bottomButton.userInteractionEnabled = NO;
            }
                break;
                
            case 1: {
                
                buttonStr = @"评价";
            }
                break;
                
            case 2: {
                
                buttonStr = @"对方否认看房";
                self.bottomButton.userInteractionEnabled = NO;
            }
                break;
                
            default:
                break;
        }
        
        [self.bottomButton setTitle:buttonStr forState:UIControlStateNormal];
    }
}

#pragma mark - 数据请求
#pragma mark -- 获取带看详情
- (void)requestTakeLookDetailData {
}

#pragma mark -- 删除带看记录
- (void)deleteTakeLook {
    
}

#pragma mark -- 带看是否属实
/**
 带看是否属实

 @param state 1：同意 2：不同意
 */
- (void)takeLookTrueOrNotWithState:(NSInteger)state {
}

#pragma mark -- 评论数据
- (void)requestCommentData {
    
}

#pragma mark -- 添加评论
- (void)addComment {
   }
#pragma mark -- 添加追加评论
- (void)addAdditionalComment {
}

#pragma mark - navigationItem
- (void)setupNavigationItems {
    [super setupNavigationItems];
    
    if (self.brokerState != 1) {
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"more..."] style:UIBarButtonItemStylePlain target:self action:@selector(rightMoreItemClickAction)];
    }
}

#pragma mark - UI
- (void)setupSubviews {
    [super setupSubviews];
    
    UIScrollView *scrollView = [[UIScrollView alloc] init];
    [self.view addSubview:scrollView];
    
    // 1. 添加一个视图，四周和 scrollView 一致
    SISSBaseView *containerView = [[SISSBaseView alloc] init];
    containerView.backgroundColor = [UIColor whiteColor];
    [scrollView addSubview:containerView];
    self.containerView = containerView;
    
    // 2. 自动布局
    WeakSelf
    [scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(weakSelf.view);
        if (@available(iOS 11.0, *)) {
            make.top.equalTo(weakSelf.mas_topLayoutGuideBottom);
            make.bottom.equalTo(weakSelf.view.mas_safeAreaLayoutGuideBottom);
        } else {
            make.top.equalTo(weakSelf.view);
            make.bottom.equalTo(weakSelf.view);
        }
    }];
    [containerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(scrollView);
        make.width.mas_equalTo(SCREEN_WIDTH);
        // 通过宽高设置 contentSize
        // make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT * 1.5));
    }];
  
    // 网络加载 --- 创建带标题的图片轮播器
    SDCycleScrollView *cycleScrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectZero delegate:self placeholderImage:[UIImage imageNamed:@"图片加载占位符"]];
    cycleScrollView.autoScroll = NO;
    cycleScrollView.bannerImageViewContentMode = UIViewContentModeScaleAspectFill;
    cycleScrollView.pageControlStyle = SDCycleScrollViewPageContolStyleNone;
    cycleScrollView.itemDidScrollOperationBlock = ^(NSInteger currentIndex) {
        
        self.pageLabel.text = [NSString stringWithFormat:@" %@/%@ ",@(currentIndex + 1),@(self.imagesURLStrings.count)];
    };
    [self.containerView addSubview:cycleScrollView];
    self.cycleScrollView = cycleScrollView;

    UILabel *pageLabel = [[UILabel alloc] init];
    pageLabel.backgroundColor = [UIColor colorWithWhite:0.8 alpha:0.5];
    pageLabel.textColor = [UIColor whiteColor];
    pageLabel.text = [NSString stringWithFormat:@" %@/%@ ",@(1),@(self.imagesURLStrings.count)];
    pageLabel.textAlignment = NSTextAlignmentCenter;
    pageLabel.layer.cornerRadius = 10;
    pageLabel.layer.masksToBounds = YES;
    [cycleScrollView addSubview:pageLabel];
    self.pageLabel = pageLabel;
    
    
    UIView *infoView = [[UIView alloc] init];
    infoView.backgroundColor = [UIColor whiteColor];
    if (!self.infoView) {
        self.infoView = infoView;
    }
    [self.containerView addSubview:self.infoView];
    
    // 我对他的评价
    if (self.isCooperationIn) {
        SISSCoopCommentShowView *coopToCommentView = [[SISSCoopCommentShowView alloc] init];
        coopToCommentView.titleStr = @"我对他的评价";
        coopToCommentView.hidden = YES;
        [self.containerView addSubview:coopToCommentView];
        self.coopToCommentView = coopToCommentView;
    } else {
        
        SISSCommentShowView *toCommentShowView = [[SISSCommentShowView alloc] init];
        toCommentShowView.titleStr = @"我对他的评价";
        toCommentShowView.hidden = YES;
        [self.containerView addSubview:toCommentShowView];
        self.toCommentShowView = toCommentShowView;
    }
    
    // 他对我的评价
    SISSCoopCommentShowView *beCommentedView = [[SISSCoopCommentShowView alloc] init];
    beCommentedView.titleStr = @"他对我的评价";
    beCommentedView.isShowRightButton = YES;
    beCommentedView.delegate = self;
    beCommentedView.hidden = YES;
    [self.containerView addSubview:beCommentedView];
    self.beCommentedView = beCommentedView;
    
    UIButton *bottomButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [bottomButton setTitle:@"等待确认" forState:UIControlStateNormal];
    [bottomButton setBackgroundColor:MAIN_COLOR];
    [bottomButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [bottomButton addTarget:self action:@selector(bottomButtonClickAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:bottomButton];
    self.bottomButton = bottomButton;
    
    UIButton *trueButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [trueButton setTitle:@"带看属实" forState:UIControlStateNormal];
    [trueButton setBackgroundColor:COLOR(0xC7DE21)];
    trueButton.tag = 100;
    [trueButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [trueButton addTarget:self action:@selector(takeLookTrueOrNotButtonClickAction:) forControlEvents:UIControlEventTouchUpInside];
    trueButton.hidden = self.type == 2;
    [self.view addSubview:trueButton];
    self.trueButton = trueButton;
    
    UIButton *untrueButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [untrueButton setTitle:@"带看不属实" forState:UIControlStateNormal];
    [untrueButton setBackgroundColor:MAIN_COLOR];
    [untrueButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    untrueButton.tag = 101;
    [untrueButton addTarget:self action:@selector(takeLookTrueOrNotButtonClickAction:) forControlEvents:UIControlEventTouchUpInside];
    untrueButton.hidden = self.type == 2;
    [self.view addSubview:untrueButton];
    self.untrueButton = untrueButton;
    
}

#pragma mark - 约束
- (void)initConstraint {
    [super initConstraint];
    
    CGFloat margin = 8.0;
    CGFloat buttonHeight = 44.0;
    WeakSelf
    [self.cycleScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(weakSelf.containerView);
        make.top.equalTo(weakSelf.containerView).offset(margin);
        if (weakSelf.imagesURLStrings.count > 0) {
            make.height.mas_equalTo(20 * margin);
        } else {
            make.height.mas_equalTo(0);
        }
    }];
    
    [self.pageLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(weakSelf.cycleScrollView).offset(-2 * margin);
        make.width.mas_equalTo(6 * margin);
        make.bottom.equalTo(weakSelf.cycleScrollView).offset(-margin);
    }];
    
    [self.infoView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(weakSelf.containerView);
        make.top.equalTo(weakSelf.cycleScrollView.mas_bottom);
    }];
    
    if (self.isCooperationIn) {
        [self.coopToCommentView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(weakSelf.containerView);
            make.top.equalTo(weakSelf.infoView.mas_bottom);
        }];
    } else {
        [self.toCommentShowView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(weakSelf.containerView);
            make.top.equalTo(weakSelf.infoView.mas_bottom);
        }];
    }
    
    [self.beCommentedView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(weakSelf.containerView);
        make.bottom.equalTo(weakSelf.containerView).offset(-buttonHeight);
        if (weakSelf.isCooperationIn) {
            make.top.equalTo(weakSelf.coopToCommentView.mas_bottom);
        } else {
            make.top.equalTo(weakSelf.toCommentShowView.mas_bottom);
        }
    }];
    
    [self.bottomButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(weakSelf.view);
        make.height.mas_equalTo(buttonHeight);
        if (@available(iOS 11.0, *)) {
            make.bottom.equalTo(weakSelf.view.mas_safeAreaLayoutGuideBottom);
        } else {
            make.bottom.equalTo(weakSelf.view);
        }
    }];
    
    // 带看属实
    [self.trueButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.view);
        make.right.equalTo(weakSelf.view.mas_centerX);
        make.height.mas_equalTo(buttonHeight);
        if (@available(iOS 11.0, *)) {
            make.bottom.equalTo(weakSelf.view.mas_safeAreaLayoutGuideBottom);
        } else {
            make.bottom.equalTo(weakSelf.view);
        }
    }];
    // 带看不属实
    [self.untrueButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(weakSelf.view);
        make.left.equalTo(weakSelf.view.mas_centerX);
        make.height.mas_equalTo(buttonHeight);
        if (@available(iOS 11.0, *)) {
            make.bottom.equalTo(weakSelf.view.mas_safeAreaLayoutGuideBottom);
        } else {
            make.bottom.equalTo(weakSelf.view);
        }
    }];
}

@end
